<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Repositories\GroupOrderSetting;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class GroupOrderSettingController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GroupOrderSetting(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->disableCreateButton();
            $grid->disableFilterButton();
            $grid->disableRefreshButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->disableDeleteButton();
            $grid->column('win_interval');
            $grid->column('session_setting');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GroupOrderSetting(), function (Show $show) {
            $show->field('id');
            $show->field('win_interval');
            $show->field('session_setting');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GroupOrderSetting(), function (Form $form) {
            $form->display('id')->help('本日场次已生成，修改以下配置次日凌晨生效');
//            $form->text('win_interval', '中单间隔');
//            $form->switch('pre_sale', '开关预售')->default(0);
            $form->number('contribution_value', '拼单贡献值奖励');
//            $form->number('release_days', '拼单成功释放天数上限');
            $form->number('daily_group_limit', '每日拼单上限');
            $form->currency('points_for_sale', '挂售消耗积分');
            $form->array('winning_settings', '中奖设置', function ($table) {
                //中奖设置
                $table->select('type', '类型')->options([
                    1=>'金额',
                    2=>'分红积分'
                ]);
                $table->currency('number', '数量');
                $table->currency('bean', '通证')->default(0);
            });

            $form->display('created_at');
            $form->display('updated_at');
//            $form->saving(function (Form $form) {
//                if ($form->session_setting) {
//                    $msg = CheckRangeDate($form->session_setting);
//                    if ($msg != null) {
//                        return $form->response()->error($msg);
//                    }
//                }
//            });
            $form->saved(function (Form $form) {
                return $form->response()->success('操作成功')->redirect(request()->fullUrl() . '/edit');
            });
        });
    }
}
